<template>
  <div>
    <div class="goods">
      <div class="menu-wrapper" ref="menuwrapper">
        <ul>
          <li v-for="(item,index) in goods" :key="item.name" class="menu-item"
          :class="{'current':currentIndex === index}" @click="selectMenu(index)">
            <span class="text" border-1px>
              <span v-show="item.type>0" class="icon" :class="classMap[item.type]"></span>
              {{item.name}}
            </span>
          </li>
        </ul>
      </div>
      <div class="foods-wrapper" ref="foodswrapper">
        <ul>
          <li v-for="item in goods" :key="item.name" class="foods-list food-list-hook">
            <h1 class="title">{{item.name}}</h1>
            <ul>
              <li v-for="food in item.foods" :key="food.name" @click="selectFood(food)" class="food-item border-1px">
                <div class="icon">
                  <img :src="food.icon" width="64" height="64">
                </div>
                <div class="content">
                  <h2 class="name">{{food.name}}</h2>
                  <p class="desc">{{food.description}}</p>
                  <div class="extra">
                    <span class="count">月销{{food.sellCount}}</span><span>好评率{{food.rating}}%</span>
                  </div>
                  <div class="price">
                    <span class="now">￥{{food.price}}</span>
                    <span class="old" v-show="food.oldPrice">￥{{food.oldPrice}}</span>
                  </div>
                  <div class="cartcontrol-wrapper">
                    <cartcontrol :food="food"  @cartadd="addfood"></cartcontrol>
                  </div>
                </div>
              </li>
            </ul>
          </li>
        </ul>
      </div>
      <shopcart :select-foods="selectFoods" ref="shopcart"></shopcart>
    </div>
    <food :food="selectedFood" ref="food" @cartadd="addfood"></food>
  </div>
</template>

<script>
import Cartcontrol from './Cartcontrol'
import BScroll from 'better-scroll'
import Shopcart from './Shopcart'
import Food from './Food'
import { mapState } from 'vuex'
export default {
  name: 'Goods',
  components: {
    Shopcart,
    Cartcontrol,
    Food
  },
  data () {
    return {
      listHeight: [],
      scrollY: 0,
      selectedFood: {}
    }
  },
  watch: {
    goods: function () {
      this.$nextTick(function () {
        this.calculateHeight()
        this.initScroll()
      })
    }
  },
  computed: {
    ...mapState({goods: 'goods'}),
    currentIndex () {
      for (let i = 0; i < this.listHeight.length; i++) {
        let height1 = this.listHeight[i]
        let height2 = this.listHeight[i + 1]
        if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
          return i
        }
      }
      return 0
    },
    selectFoods () {
      let foods = []
      this.goods.forEach((good) => {
        good.foods.forEach((food) => {
          if (food.count) {
            foods.push(food)
          }
        })
      })
      return foods
    }
  },
  methods: {
    selectMenu (index) {
      let foodlist = this.$refs.foodswrapper.getElementsByClassName('food-list-hook')
      let el = foodlist[index]
      this.foodsScroll.scrollToElement(el, 200)
    },
    initScroll () {
      this.menuScroll = new BScroll(this.$refs.menuwrapper, {click: true})
      this.foodsScroll = new BScroll(this.$refs.foodswrapper, {click: true, probeType: 3})
      this.foodsScroll.on('scroll', (pos) => {
        this.scrollY = Math.abs(Math.round(pos.y))
      })
    },
    calculateHeight () {
      let foodlist = this.$refs.foodswrapper.getElementsByClassName('food-list-hook')
      let height = 0
      this.listHeight.push(height)
      for (let i = 0; i < foodlist.length; i++) {
        let item = foodlist[i]
        height += item.clientHeight
        this.listHeight.push(height)
      }
    },
    addfood (target) {
      this._drop(target)
    },
    _drop (target) {
      this.$nextTick(() => {
        this.$refs.shopcart.drop(target)
      })
    },
    selectFood (food) {
      this.selectedFood = food
      this.$refs.food.show()
    }
  },
  created () {
    this.classMap = ['decrease', 'discount', 'special', 'invoice', 'guarantee']
  }
}
</script>

<style lang="stylus" scoped>
@import '../../assets/styles/maxin.styl'
  .goods
    display flex
    position absolute
    top 3.48rem
    bottom .92rem
    width 100%
    overflow hidden
    .menu-wrapper
      flex 0 0 1.6rem
      width 1.6rem
      background #f3f5f7
      .menu-item
        display table
        height 1.08rem
        width 1.12rem
        line-height .28rem
        padding 0 .24rem
        &.current
          position relative
          z-index 10
          margin-top -.01rem
          background #fff
          font-weight 700
          .text
            border-none()
        .icon
          display inline-block
          vertical-align top
          width .24rem
          height .24rem
          margin-right .04rem
          background-size .24rem .24rem
          background-repeat no-repeat
          &.decrease
            bg-image('decrease_3')
          &.discount
            bg-image('discount_3')
          &.guarantee
            bg-image('guarantee_3')
          &.invoice
            bg-image('invoice_3')
          &.special
            bg-image('special_3')
        .text
          display table-cell
          width 1.12rem
          vertical-align middle
          border-1px(rgba(7,17,27,0.1))
          font-size .24rem
    .foods-wrapper
      display flex 1
      .title
        padding-left .28rem
        height .52rem
        line-height .52rem
        border-left .04rem solid #d9dde1
        font-size .24rem
        color rgb(147,153,159)
        background #f3f5f7
      .food-item
        display flex
        margin .36rem
        padding-bottom .36rem
        border-1px(rgba(7,17,27,0.1))
        &:last-child
          border-none()
          margin-bottom 0
        .icon
          flex 0 0 1.04rem
          margin-right .2rem
        .content
          flex 1
          .name
            font-size .28rem
            height .28rem
            margin .04rem 0 .16rem 0
            line-height .28rem
            color rgb(7,17,27)
          .desc, .extra
            line-height .2rem
            font-size .2rem
            color rgb(147,153,159)
          .desc
            line-height .24rem
            margin-bottom .16rem
          .extra
            line-height .2rem
            .count
              margin-right .24rem
          .price
            font-weight 700
            line-height .48rem
            .now
              margin-right .16rem
              font-size .28rem
              color rgb(240,20,20)
            .old
              text-decoration line-through
              font-size .2rem
              color rgb(147,153,159)
          .cartcontrol-wrapper
            position absolute
            right 0
            bottom .24rem
</style>
